package com.simple.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.simple.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.simple.common.annotation.Log;
import com.simple.common.core.controller.BaseController;
import com.simple.common.core.domain.AjaxResult;
import com.simple.common.enums.BusinessType;
import com.simple.project.domain.Tag;
import com.simple.project.service.ITagService;
import com.simple.common.utils.poi.ExcelUtil;
import com.simple.common.core.page.TableDataInfo;

/**
 * 博客标签Controller
 * 
 * @author simple
 * @date 2020-12-01
 */
@RestController
@RequestMapping("/project/tag" )
public class TagController extends BaseController {

    @Autowired
    private ITagService iTagService;

    /**
     * 查询博客标签列表
     */
    @PreAuthorize("@ss.hasPermi('project:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tag tag)
    {
        startPage();
        LambdaQueryWrapper<Tag> lqw = Wrappers.lambdaQuery(tag);
        if (StringUtils.isNotBlank(tag.getTagName())){
            lqw.like(Tag::getTagName ,tag.getTagName());
        }
        if (tag.getIsDeleted() != null){
            lqw.eq(Tag::getIsDeleted ,tag.getIsDeleted());
        }
        List<Tag> list = iTagService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出博客标签列表
     */
    @PreAuthorize("@ss.hasPermi('project:tag:export')" )
    @Log(title = "博客标签" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Tag tag) {
        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<Tag>(tag);
        List<Tag> list = iTagService.list(lqw);
        ExcelUtil<Tag> util = new ExcelUtil<Tag>(Tag. class);
        return util.exportExcel(list, "tag" );
    }

    /**
     * 获取博客标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:tag:query')" )
    @GetMapping(value = "/{tagId}" )
    public AjaxResult getInfo(@PathVariable("tagId" ) Long tagId) {
        return AjaxResult.success(iTagService.getById(tagId));
    }

    /**
     * 新增博客标签
     */
    @PreAuthorize("@ss.hasPermi('project:tag:add')" )
    @Log(title = "博客标签" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tag tag) {
        return toAjax(iTagService.save(tag) ? 1 : 0);
    }

    /**
     * 修改博客标签
     */
    @PreAuthorize("@ss.hasPermi('project:tag:edit')" )
    @Log(title = "博客标签" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tag tag) {
        return toAjax(iTagService.updateById(tag) ? 1 : 0);
    }

    /**
     * 删除博客标签
     */
    @PreAuthorize("@ss.hasPermi('project:tag:remove')" )
    @Log(title = "博客标签" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagIds}" )
    public AjaxResult remove(@PathVariable Long[] tagIds) {
        return toAjax(iTagService.removeByIds(Arrays.asList(tagIds)) ? 1 : 0);
    }
}
